﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace DataModel
{
    public class TempratureModel
    {
        public bool IsValid { get; set; }
        public bool IsEnable { get; set; } = true;
        public int ContinuousFailCount { get; set; }

        private int cabelId;
        public List<double> Values;
        public DateTime UpdatedTime;

        public TempratureModel(byte cabelId)
        {
            this.cabelId = cabelId;
            Values = new double[15] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }.ToList();
            UpdatedTime = DateTime.Now;
        }

        public TempratureModel(byte cabelId, short[] _values)
        {
            //this.IsValid = _valid;
            this.cabelId = cabelId;
            Values = _values.Select(v=> { return Convert.ToDouble(v/10d); }).ToList();
            UpdatedTime = DateTime.Now;
        }

        public double Average
        {
            get
            {
                return GetAverage();
            }
        }

        private double GetAverage()
        {
            if (Values == null || !Values.Any())
                return 0d;
            double average = 0d;
            int validCount = 0;
            foreach (double d in Values)
            {
                if (d != 0)
                {
                    average += d;
                    validCount++;
                }
            }
            if (validCount == 0)
                return 0d;

            return average / validCount;
        }

        public double Max
        {
            get {

                return GetMax(); 
            }
        }

        private double GetMax()
        {
            if (Values == null)
                return 0d;
            if (!Values.Any())
                return 0d;

            return Values.Max();
        }

        public int CableId
        {
            get
            {
                if (cabelId > 33)
                {
                    return cabelId - 33;
                }
                else
                {
                    return cabelId;
                }
            }
        }
    }
}